package com.fzc.rhinooffice.module.workbench;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.fzc.rhinooffice.R;
import com.fzc.rhinooffice.common.RemoteInvoke;
import com.fzc.rhinooffice.common.utils.StringUtil;
import com.fzc.rhinooffice.common.view.CustomProgress;
import com.fzc.rhinooffice.module.BaseActivity;
import com.fzc.rhinooffice.module.CommonAdapter;
import com.fzc.rhinooffice.module.ViewHolder;
import com.fzc.rhinooffice.module.entity.FileBean;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cong on 15/6/10.
 */
@ContentView(R.layout.activity_filemanager)
public class FileManagerActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    @ViewInject(R.id.gv_files)
    private GridView gv_files;

    @ViewInject(R.id.tv_title)
    private TextView tv_title;

    private CommonAdapter<FileBean> commonAdapter;

    private List<FileBean> fileList;
    private int page = 1;

    private int mf_id = 0;
    private int pre_mf_id = 0;

    private int fail_mf_id = 0;
    private CustomProgress customProgress;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        customProgress = CustomProgress.init(this,
                getResources().getString(R.string.loading), false, null);
        initUI();
    }

    private void initUI() {
        tv_title.setText("文件柜");
        gv_files.setOnItemClickListener(this);
        getFile();
    }

    private void getFile() {
        customProgress.show();

        /**
         * mf_id
         * */
        RemoteInvoke.file(mHandler, mf_id, page);
    }

    private void initAdapter() {
        if (commonAdapter != null && mf_id != 0) {
            FileBean fileBean = new FileBean();
            fileBean.setMfId(pre_mf_id + "");
            LogUtils.e(pre_mf_id + "");
            fileBean.setMfName("...");
            fileBean.setMfType("m");
            fileList.add(0, fileBean);
        }
        commonAdapter = new CommonAdapter<FileBean>(this, fileList, R.layout.file_item) {

            @Override
            public void convert(ViewHolder holder, FileBean item) {
                holder.setText(R.id.tv_file, item.getMfName());
                LogUtils.e(item.getMfType());
/**
 * [{"mf_type":"如值为m则记录为目录并点这行时用mf_id可查下级目录或文件，如为f则此记录为文件并可能有多个附件",
 * "mf_id":"目录或文件ID",
 * "mf_name":"目录或文件名称",
 * "user_id":"发布人",
 * "send_time":"发布时间",
 * "attachment_rows":"如果是文件有附件则是附件数量",
 * "attachment_record":[{"url":"下载网址","name":"附件名称"}]}
 * */

            }
        };
        gv_files.setAdapter(commonAdapter);

//        } else {
//            LogUtils.e("刷新");
//            commonAdapter.notifyDataSetChanged();
//        }
    }


    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            customProgress.dismiss();
            switch (msg.what) {
                case 1: {
                    JSONObject jsonObject = (JSONObject) msg.obj;
                    if (jsonObject != null) {

                        try {
                            if (jsonObject.getString("record") != null
                                    && !"".equals(jsonObject.getString("record"))) {


//                                if (fileList == null||page==1) {
                                fileList = new ArrayList<FileBean>();
//                                }
                                JSONArray jsonArray = jsonObject.getJSONArray("record");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    jsonObject = (JSONObject) jsonArray.get(i);
                                    FileBean fileBean = new FileBean(jsonObject);
                                    fileList.add(fileBean);
                                }


                                if (fileList != null && fileList.size() != 0) {
                                    initAdapter();
                                } else {
                                    StringUtil.showToast("暂无文件");
                                    pre_mf_id = 0;
                                }
                            } else {
                                StringUtil.showToast("暂无文件");
                                pre_mf_id = 0;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            StringUtil.showToast("未知错误");
                            pre_mf_id = 0;
                        }
                    } else {
                        StringUtil.showToast("未知错误");
                        pre_mf_id = 0;

                    }
                }
                break;

                case -1:
                    StringUtil.showToast("加载失败！");
                    pre_mf_id = 0;
                    break;
            }
        }


    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        FileBean fileBean = fileList.get(position);

        pre_mf_id = mf_id;

        mf_id = Integer.parseInt(fileBean.getMfId());
        getFile();

    }

    @OnClick({R.id.iv_back, R.id.tv_title})
    private void goBack(View v) {
        super.back();
    }
}